package com.mktech.taralib.service;

import com.mktech.taralib.util.LogTool;

import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;

public class Tara
{
	private static ITaraManager mInstance;
	
	private static ServiceConnection mConnect = new ServiceConnection()
	{

		@Override
		public void onServiceConnected(ComponentName name, IBinder service)
		{
			// TODO Auto-generated method stub
			mInstance = ITaraManager.Stub.asInterface(service);
			LogTool.i("onServiceConnected-----mInstance = "+mInstance);
		}

		@Override
		public void onServiceDisconnected(ComponentName name)
		{
			// TODO Auto-generated method stub
			LogTool.i("onServiceDisconnected");
			mInstance = null;
		}
		
	};
	
	public static void init(Context cx)
	{
		if(mInstance == null)
		{
			Intent intent = new Intent(ITaraManager.class.getName());
			cx.getApplicationContext().bindService(intent, mConnect, Service.BIND_AUTO_CREATE);
		}
	}
	
	public static void deInit(Context cx)
	{
		if(mInstance != null)
		{
			cx.unbindService(mConnect);
			mInstance = null;
		}
	}
	
	public static boolean isInit()
	{
		return mInstance != null;
	}
	
	public static ITaraManager getInterface()
	{
		return mInstance;
	}
}
